<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #create_time="{row}">
      <div class="flex">
        <radio :config="row.config" v-model:value="row.value" button="1"></radio>
        <date_between :name="row.name" config="Y-M-D" v-model:value="row.value"></date_between>
      </div>
    </template>
    <template v-slot:pay_user="{row}">
      <div class="flex" v-if="row.pay_user" style="align-items: center;">
        <img :src="row.pay_user['头像']" style="height: 35px;">
        <span>{{row.pay_user['昵称']}}</span>
      </div>
    </template>
    <template #action="{row}">
      <p v-if="row.btns.length==0">暂无操作</p>
      <el-button v-for="btn in row.btns" :type="btn.type" size="small" @click="btn.onclick(row)">{{ btn.title }}</el-button>
    </template>
  </list_url>
</template>
<script>
export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');

    let status_config = this.admin_config('status_alias_map.order.status');
    let refund_status_config = this.admin_config('status_alias_map.order.refund_status');
    var order_refund_status_config = app.admin_config('status_alias_map.order_refund.status');
    function date_range_map(){
      let now_time = new Date();
      let day_week = now_time.getDay(); // 获取今天是星期几（0-6，其中0为星期日）
      day_week = day_week==0 ? 6 : day_week-1; //0表示星期一
      let day_month = now_time.getDate(); // 获取今天是当月的第几天（1-31）
      let year = now_time.getFullYear();
      let map = {
        'today': s94Mjs.date('Y-M-D', now_time),
        'today-1': s94Mjs.date('Y-M-D', now_time.getTime()-1000*3600*24 ),
        'today+1': s94Mjs.date('Y-M-D', now_time.getTime()+1000*3600*24 ),
        'week0': s94Mjs.date('Y-M-D', now_time.getTime()-1000*3600*24*day_week ),
        'week7': s94Mjs.date('Y-M-D', now_time.getTime()+1000*3600*24*(7-day_week) ),
        'month': s94Mjs.date('Y-M-01', now_time),
        'month+1': s94Mjs.date('Y-M-01', now_time.getTime()+1000*3600*24*(32-day_month) ),
      }
      let res = [
        {value:[map['today'],map['today+1']]+'', content:'今天'},
        {value:[map['today-1'],map['today']]+'', content:'昨天'},
        {value:[map['week0'],map['week7']]+'', content:'本周'},
        {value:[map['month'],map['month+1']]+'', content:'本月'},
        {value:`${year}-01-01,${year+1}-01-01`, content:'本年'},
      ];
      return res;
    }
    status_config = app.decode_config(status_config);
    status_config.unshift({value:"", content:"全部"});
    refund_status_config = app.decode_config(refund_status_config);
    refund_status_config.unshift({value:"", content:"全部"});

    let forms_config = {
      create_time: {label:'下单时间',name:'where[create_time][between]',type:'create_time',config:date_range_map(),value:''},
      status: {label:'订单状态',name:'where[status][eq]',type:'radio',config:status_config,value:'',button:true},
      refund_status: {label:'退款状态',name:'where[refund_status][eq]',type:'radio',config:refund_status_config,value:'',button:true},
      order_pay_order_no: {label:'订单号',name:'where[order_pay.order_no][like]',type:'text_one',value:''},
      tenant_id: {label:'租户ID',name:'where[tenant_id][eq]',type:'select_one',config:[],value:''},
      tenant_package_id: {label:'版本',name:'where[tenant_package_id][eq]',type:'select_one',config:[],value:''},
    };
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', type: 'hidden', order:true},
      order_pay_order_no:{name:'order_pay_order_no', label:'订单号', width:'190px'},
      tenant_title:{name:'tenant_title', label:'租户', width:'120px'},
      title:{name:'title', label:'版本', width:'100px'},
      timeout:{name:'timeout', label:'时长', width:'120px', type: 'duration'},
      pay_money:{name:'pay_money', label:'支付金额', width:'100px'},
      order_pay_pay_type:{name:'order_pay_pay_type', label:'支付方式', type:'select_one', config:this.admin_config('status_alias_map.order_pay.pay_type',[]), width:'70px'},
      status:{name:'status', label:'状态', width:'70px', type:'select_one', config:status_config},
      refund_status:{name:'order_refund_status', label:'退款状态', width:'70px', type:'select_one', config:order_refund_status_config},
      pay_user:{name:'pay_user', label:'支付用户', type: 'pay_user', min_width:'100px'},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'创建时间', width:'165px'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'170px', type:'action', fixed:'right'};

    function action_btns(row){
      let res = {};
      if (row.order_refund_id>0) res['退款记录'] = function (){
        return app.to_refund_log(row);
      }
      if (row.order_refund_status==0) res['审核退款'] = function (){
        return app.iframe('/order_refund/edit?id='+row.order_refund_id, {width:'750px'});
      }
      if(row.order_refund_status==3 || row.order_refund_status==6) res['发起退款'] = function (){
        return app.refund(row);
      }
      return app.decode_btns(res);
    }

    return {
      config: {
        api: '/admin/tenant_package_order/'+(is_del?'recycle':'list'),
        title: "租户套餐订单列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
        s94Mjs.each(list, function (row){
          if (row.refund_status==0) row.order_refund_status = null;
         	row.pay_user = false;
         	try {
         		if (row.note_user) row.pay_user = JSON.parse(row.note_user);
         	} catch (e) {}
         	row.btns = action_btns(row);
         })
         return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
    }
  },
  methods: {
    to_edit(row){
      let can_edit = in_right_admin('TenantPackageOrder/edit') && ~[0,row.tenant_id].indexOf(this.admin_config('this_tenant.id'))
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      this.iframe('edit?'+s94Mjs.param_encode(param))
    },
    to_refund_log(row){
      this.iframe('/order/refund_log?model_name=tenant_package&model_alias=租户&id='+row.id)
    },
    refund(row){
      let app = this;
      function action(){
        ajax('/admin/order_refund/edit_refund?id='+row.order_refund_id, function(res){
          app.$refs['list_url'].list();
        })
      }
      if(!row.order_refund_need_deliver) return this.$confirm(`是否发起退款？`).then(()=>{
        action();
      }).catch(()=>{})
      if (!row.order_refund_deliver_no) return this.$message.error('需要客户退货后才能发起退款');
      this.$confirm(`此退款单需要用户退货，退货单号(${row.order_refund_deliver_no})，是否已收到货物`).then(()=>{
        action()
      }).catch(()=>{})
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

  }
}
</script>
